#!/usr/bin/env python

__author__ = "Juan C. Montes"
__credits__ = ["Marcos Orallo", "Juan C. Montes"]
__license__ = "GPL"
__version__ = "0.0.1"
__maintainer__ = "Juan C. Montes"
__email__ = "jcmontes.tec@gmail.com"


# Ogre3D References
import ogre.renderer.OGRE as ogre
import ogre.io.OIS as ois
import ogre.gui.CEGUI as cegui

# Project References


class menu:
    """Generic class to manage all menus.
Can show options, retrieve the selected element, ...
    """
    
    def __init__(self, width, height):
        """Default constructor"""
        # Properties
        self.renderer = cegui.Renderer
        self.system = cegui.System
        self.window = ogre.Windows
        self.scenemanager = None
        self.width = width
        self.height = height
        
        # Private variables
        self._itemscount = 0
        pass
    
    def createMenu(self, window, sm):
        """Prepare all menu data"""    
        self.window = window
        self.scenemanager = sm    
        self.renderer = cegui.OgreCEGUIRenderer(self.window,
                                                ogre.RENDER_QUEUE_OVERLAY,
                                                False,
                                                3000,
                                                self.scenemanager)
        self.system = cegui.System(self.renderer)
        
        # Load the Menu schema
        cegui.SchemeManager.getSingleton().loadScheme("PyGUISkin.scheme")
        self.system.setDefaultFont("BlueHighway-12")        
        pass
    
    def createSceneMenu(self):
        """Load all data to show the menu"""
        windowManager = cegui.WindowManager
        windowManager = cegui.WindowManager.getSingleton()
        sheet = windowManager.createWindow("DefaultGUISheet", 
                                           "MainSheet")
        self.system.setGUISheet(sheet)
        pass
    
    def addNewItem(self, text):
        """Add a new button to menu"""
        windowManager = cegui.WindowManager
        windowManager = cegui.WindowManager.getSingleton()
        newButton = cegui.Window
        newButton = windowManager.createWindow("PyGUI/Button", text)
        newButton.setText(text)
        newButton.setSize(cegui.UVector2(cegui.UDim(0.15, 0),
                                         cegui.UDim(0.05, 0)))
        x = (self.width/2) - (newButton.getSize().d_x.d_offset * newButton.getMaxSize().d_x.d_scale /2)        
        print ("%s: %i" % (text,x))
        newButton.setPosition(cegui.UVector2(cegui.UDim(0, 400),
                                             cegui.UDim(0, self._itemscount*20)))
        self.system.getGUISheet().addChildWindow(newButton)
        self._itemscount += 1 
        pass
        
    